package com.valentinalee.bms.web.servlet.filemanage;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;

import com.valentinalee.bms.entity.FileSystemVO;
import com.valentinalee.bms.entity.ManagerVO;
import com.valentinalee.bms.service.FileSystemService;
import com.valentinalee.bms.service.impl.FileSystemServiceImpl;

public class FileDownloadServlet extends HttpServlet {
	FileSystemService iFileSystemService = new FileSystemServiceImpl();
	/**
	 * Constructor of the object.
	 */
	public FileDownloadServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ManagerVO mv = null;
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String tName = request.getParameter("filename");
		String dlt ="";
		//解决servlet请求里参数乱码的情况
		String fileName = new String(tName.getBytes("iso-8859-1"),"UTF-8");
		String path ="E:\\二阶段项目\\bms\\WebRoot\\WEB-INF\\upload";
		boolean isSuccessed = false;
		File file = new File(path,fileName);
		if(!file.exists()){
			response.getWriter().write("文件不存在");
			return;
		}else{
			response.setContentType("image/jpg");
			response.setHeader("Content-Disposition", "attachment; filename="+fileName);
			IOUtils.copy(new FileInputStream(file), response.getOutputStream());
			isSuccessed=true;
			Date d = new Date();
			//通过session获取下载人的信息
			HttpSession session = request.getSession(true);
			mv = (ManagerVO) session.getAttribute("currentManager");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dlt = sdf.format(d);
			
		}
		if(isSuccessed){
			//调用service层完成下载记录的添加
			//第一步,装载FileSystem的实体类
			FileSystemVO fsv = new FileSystemVO();
			String handleFlag = "dt";
			fsv.setHandleFlag(handleFlag);
			fsv.setFileUser(mv.getName());
			fsv.setFilePath(path);
			fsv.setFileName(tName);
			fsv.setHandleTime(dlt);
			try {
				FileSystemVO fsv1 = iFileSystemService.addFileRmark(fsv);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
